package com.ruoyi.notice.service.impl;

import java.util.List;

import com.ruoyi.notice.mapper.TotalAmountMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.notice.mapper.WithdrawalPCMapper;
import com.ruoyi.notice.domain.WithdrawalPC;
import com.ruoyi.notice.service.IWithdrawalPCService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 提现Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-05-19
 */
@Service
public class WithdrawalPCServiceImpl implements IWithdrawalPCService 
{
    @Autowired
    private WithdrawalPCMapper withdrawalPCMapper;
    @Autowired
    private TotalAmountMapper totalAmountMapper;

    /**
     * 查询提现
     * 
     * @param id 提现主键
     * @return 提现
     */
    @Override
    public WithdrawalPC selectWithdrawalPCById(Long id)
    {
        return withdrawalPCMapper.selectWithdrawalPCById(id);
    }

    /**
     * 查询提现列表
     * 
     * @param withdrawalPC 提现
     * @return 提现
     */
    @Override
    public List<WithdrawalPC> selectWithdrawalPCList(WithdrawalPC withdrawalPC)
    {
        return withdrawalPCMapper.selectWithdrawalPCList(withdrawalPC);
    }

    /**
     * 新增提现
     * 
     * @param withdrawalPC 提现
     * @return 结果
     */
    @Override
    public int insertWithdrawalPC(WithdrawalPC withdrawalPC)
    {
        return withdrawalPCMapper.insertWithdrawalPC(withdrawalPC);
    }

    /**
     * 修改提现
     * 
     * @param withdrawalPC 提现
     * @return 结果
     */
    @Override
    @Transactional
    public int updateWithdrawalPC(WithdrawalPC withdrawalPC)
    {
        //先查寻数据库中的该条记录
        WithdrawalPC withdrawalPC1 = withdrawalPCMapper.selectWithdrawalPCById(withdrawalPC.getId());
        if (withdrawalPC1.getStatus() == 1 || withdrawalPC1.getStatus() == 2) {
            throw new RuntimeException("该提现已经审核过了");
        }
        //查看提现状态  0审核中直接修改  1审核通过直接修改  2审核不通过要返还余额
        if (withdrawalPC.getStatus() == 2) {
            //审核不通过
            //查询用户的余额
            Double balance = totalAmountMapper.getBalance(withdrawalPC.getMerchantId());
            //修改用户余额
            totalAmountMapper.updateBalance(Math.toIntExact(withdrawalPC.getMerchantId()),balance + withdrawalPC.getCashWithdrawal());
        }
        return withdrawalPCMapper.updateWithdrawalPC(withdrawalPC);
    }

    /**
     * 批量删除提现
     * 
     * @param ids 需要删除的提现主键
     * @return 结果
     */
    @Override
    public int deleteWithdrawalPCByIds(Long[] ids)
    {
        //先查寻数据库中的该条记录
        for (Long id : ids) {
            WithdrawalPC withdrawalPC = withdrawalPCMapper.selectWithdrawalPCById(id);
            if (withdrawalPC.getStatus() == 0) {
                throw new RuntimeException("该提现未经审核！！！");
            }
        }
        return withdrawalPCMapper.deleteWithdrawalPCByIds(ids);
    }

    /**
     * 删除提现信息
     * 
     * @param id 提现主键
     * @return 结果
     */
    @Override
    public int deleteWithdrawalPCById(Long id)
    {
        //先查寻数据库中的该条记录
        WithdrawalPC withdrawalPC = withdrawalPCMapper.selectWithdrawalPCById(id);
        if (withdrawalPC.getStatus() == 0) {
            throw new RuntimeException("该提现未经审核！！！");
        }
        return withdrawalPCMapper.deleteWithdrawalPCById(id);
    }
}
